package bCommon
{
	public class StringUtil
	{
		public function StringUtil()
		{
		}
		
		/**
		 * 对字符串左补位
		 */ 
		public static function format(source:String,length:uint,ss:String):String
		{
			if(source==null)
				return null;
			if(source.length>=length)
				return source;
			
			var slen:uint=source.length;	
			for(var i=0;i<length-slen;i++)
			{
				source=ss+source;
			}
			return source;
				
		}
		
		public static function unicodeString(source:String):String
		{
			var str:String=new String();
			for(var i=0;i<source.length;i++)
			{
				var charcode:Number=source.charCodeAt(i);
				
				str+="\\u"+format(charcode.toString(16),4,"0");
			}
			return str;
			
		}
		
		public static function toString(s:Object):String
		{
			if(s==null)
			{
				s="";
			}	
			
			return s.toString();
		}
		
		public static function BeginWith(buf:String,substr:String):Boolean
		{
			if (buf.substr(0,substr.length)==substr)
			  return true;
			return false;
		}
		
		public static function escapeNull(obj:Object,retStr:String=""):String{
			if(!obj) return retStr;
			if(obj==null) return retStr;
			if(obj=="null") return retStr;
			if(obj=="undefined") return retStr;
			return obj as String;
		}
		
		public static function checkTimeHH24MI(timeStr:String):Boolean{
			if(timeStr) return false;
			var timeArr:Array=timeStr.split(":");
			if(timeArr.length!=2) return false;
			var hour:uint=parseInt(timeArr[0]);
			var min:uint=parseInt(timeArr[1]);
			if(hour>12||hour<0) return false;
			if(min<0||min>59) return false;
			return true;
		}
		
		public static function checkNumberFloat(floatStr:String,p:int,s:int):Boolean{
			var maxValue:int=Math.pow(10,p-s);
			var floatValue:Number=Number(floatStr);
			if(isNaN(floatValue)) return false
			if(floatValue<maxValue) return true;
			return false;
		}
		
	  /**
	   * 返回中文名称星期几
	   * @param myDate 日期型参数
	   * @return  String 几
	   */     
	    public  static function getDay(dayno:uint):String {
			var days:Array = ["星期天","星期一", "星期二", "星期三", "星期四","星期五", "星期六"];
			if (dayno > 7)
			    return "参数错误";
			return days[dayno - 1];
	    }
		
		/** 从右边取n个字符 */
		public static function RightSubstr(buf:String,length:int):String
		{
			if (buf==null) return buf;
			return buf.substr(buf.length-length,length);
		}
	}
}